﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class DiscountBaseDAL : BaseDAL<DiscountBaseDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DiscountBase model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into DiscountBase(");			
            strSql.Append("Title,TimeSpanType,TimeSpanCount,ConsumerTotalTimes,UserType,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@Title,@TimeSpanType,@TimeSpanCount,@ConsumerTotalTimes,@UserType,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@Title", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@TimeSpanType", SqlDbType.Int,4) ,            
                        new SqlParameter("@TimeSpanCount", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerTotalTimes", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserType", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.Title;                        
            parameters[1].Value = model.TimeSpanType;                        
            parameters[2].Value = model.TimeSpanCount;                        
            parameters[3].Value = model.ConsumerTotalTimes;                        
            parameters[4].Value = model.UserType;                        
            parameters[5].Value = model.IsDelete;            			var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DiscountBase model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update DiscountBase set ");
			                                                
            strSql.Append(" Title = @Title , ");                                    
            strSql.Append(" TimeSpanType = @TimeSpanType , ");                                    
            strSql.Append(" TimeSpanCount = @TimeSpanCount , ");                                    
            strSql.Append(" ConsumerTotalTimes = @ConsumerTotalTimes , ");                                    
            strSql.Append(" UserType = @UserType , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@Title", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@TimeSpanType", SqlDbType.Int,4) ,            
                        new SqlParameter("@TimeSpanCount", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerTotalTimes", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserType", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.Title;                        
            parameters[2].Value = model.TimeSpanType;                        
            parameters[3].Value = model.TimeSpanCount;                        
            parameters[4].Value = model.ConsumerTotalTimes;                        
            parameters[5].Value = model.UserType;                        
            parameters[6].Value = model.IsDelete;                        return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update DiscountBase set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update DiscountBase set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, Title, TimeSpanType, TimeSpanCount, ConsumerTotalTimes, UserType, IsDelete  ");			
			strSql.Append("  from DiscountBase ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM DiscountBase where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        public List<ViewDiscountBaseDto> GetPagingList(int pageIndex, int pageSize, string searchKeyWord, string colOrder, ref int totalCount)
        {
            string strWhere = " IsDelete = 0 ";

            if (!string.IsNullOrEmpty(searchKeyWord))
            {
                strWhere += " AND (Title LIKE '%" + searchKeyWord + "%' OR TimeSpanType LIKE '%" + searchKeyWord +
                            "%' OR TimeSpanCount LIKE '%" + searchKeyWord + "%' OR ConsumerTotalTimes LIKE '%" + searchKeyWord +
                            "%' OR UserType LIKE '%') ";
            }
            const string selectColumus = " *, (case when UserType=0 then '不限制' WHEN  UserType=1 then '平台用户' else '店铺用户' end) as UserTypeStr,('每' + cast(TimeSpanCount as nvarchar) + (case  when TimeSpanType=0 then '日' WHEN TimeSpanType=1 then '周' WHEN TimeSpanType=2 then '月' WHEN TimeSpanType=3 then '年'  end ) + cast(ConsumerTotalTimes as nvarchar) + '次') as TimeSpanTypeStr ";
            return GetSimpleListPage<ViewDiscountBaseDto>(pageIndex, pageSize, "DiscountBase", null, selectColumus, colOrder, strWhere,
                ref totalCount);
        }
		#endregion
	}
}
